package org.esprit.ocm.client.ui.one;

import org.esprit.ocm.client.rpc.oca.ImageOcaService;
import org.esprit.ocm.client.rpc.oca.ImageOcaServiceAsync;
import org.esprit.ocm.dto.impl.AwsCredentialsDto;
import org.esprit.ocm.dto.impl.ServerDto;
import org.esprit.ocm.dto.impl.UserDto;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;

public class CreateImage extends Window {

	private final ImageOcaServiceAsync	imageOcaService	= GWT.create(ImageOcaService.class);
	
	public CreateImage(final ServerDto server, final AwsCredentialsDto credentials, final UserDto user) {
		setTitle("Create Image");
		setWidth(300);
		setHeight(450);
		setIsModal(true);
		setShowModalMask(true);
		setShowCloseButton(true);
		setShowMinimizeButton(false);
		centerInPage();

		final DynamicForm form = new DynamicForm();
		form.setWidth(250);
		form.setHeight(400);
		form.setPadding(5);
		form.setLayoutAlign(VerticalAlignment.TOP);

		final TextItem name = new TextItem("name");
		name.setTitle("Name");
		name.setRequired(true);

		final TextAreaItem description = new TextAreaItem("description");
		description.setTitle("Description");
		description.setDefaultValue("");
		description.setRequired(true);

		final ComboBoxItem CB1 = new ComboBoxItem();
		CB1.setTitle("<b>Type:<b>");
		CB1.setDefaultValue("OS");
		CB1.setValueMap("OS", "CD-ROM", "DATABlock");

		final RadioGroupItem publicRG = new RadioGroupItem();
		publicRG.setName("Public");
		publicRG.setRequired(true);
		publicRG.setVertical(false);
		publicRG.setDefaultValue("NO");
		publicRG.setValueMap("YES", "NO");
		publicRG.setRedrawOnChange(true);

		final RadioGroupItem persistentRG1 = new RadioGroupItem();
		persistentRG1.setName("Persistent");
		persistentRG1.setRequired(true);
		persistentRG1.setVertical(false);
		persistentRG1.setDefaultValue("NO");
		persistentRG1.setValueMap("YES", "NO");
		persistentRG1.setRedrawOnChange(true);

		final TextItem prefix = new TextItem("prefix");
		prefix.setTitle("Device prefix:");
		prefix.setDefaultValue(" ");
//		prefix.setRequired(true);

		final ComboBoxItem CB2 = new ComboBoxItem();
		CB2.setTitle("<b>Bus:<b>");
		CB2.setDefaultValue("IDE");
		CB2.setValueMap("IDE", "SCSI", "Virtio(KVM)");

		final RadioGroupItem RG2 = new RadioGroupItem();
		RG2.setTitle("Path vs. source: ");
		RG2.setRequired(true);
		RG2.setVertical(true);
		RG2.setValueMap("Path", "Source");
		RG2.setRedrawOnChange(true);

		final TextItem pathItem = new TextItem();
		pathItem.setName("Path");
		pathItem.setDefaultValue("");
		pathItem.setTitle("Path");

		final TextItem sourceItem = new TextItem();
		sourceItem.setName("Source");
		sourceItem.setDefaultValue("");
		sourceItem.setTitle("Source");

		RG2.addChangedHandler(new ChangedHandler() {
			
			public void onChanged(ChangedEvent event) {
				if (RG2.getValue().toString().equals("Path")) {
					pathItem.enable();
					sourceItem.disable();
				}
				if (RG2.getValue().toString().equals("Source")) {
					sourceItem.enable();
					pathItem.disable();
				}
			}
		});

		ButtonItem createButton = new ButtonItem("create", "Create");
		createButton.setAlign(Alignment.CENTER);
		createButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {

				String template = "";
				// TODO if

				template += "NAME = \"" + name.getValue().toString() + "\"" + "\n";
				template += "DESCRIPTION = \"" + description.getValue().toString() + "\"" + "\n";
				template += "TYPE = " + CB1.getValue().toString() + "\n";
				template += "PUBLIC = " + publicRG.getValue().toString() + "\n";
				template += "PERSISTENT = " + persistentRG1.getValue().toString() + "\n";
				template += "DEV_PREFIX = \"" + prefix.getValue().toString() + "\"" + "\n";
				template += "BUS = " + CB2.getValue().toString() + "\n";

				if (sourceItem.isDisabled()) {

					template += "PATH = " + pathItem.getValue().toString() + "\n";
				}
				if (pathItem.isDisabled()) {

					template += "SOURCE = " + sourceItem.getValue().toString() + "\n";
				}

				System.out.println(template);
				

				String secret = credentials.getAwsAccessKeyId() + ":" + credentials.getSecretAccessKey();
				String endpoint = server.getIpServer();
				imageOcaService.allocate(secret, endpoint, template, new AsyncCallback<String>() {
					
					public void onSuccess(String s) {
						SC.warn(s);
						destroy();
					}
					
					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub
						
					}
				});
			}
		});

		form.setFields(name, description, CB1, publicRG, persistentRG1, prefix, CB2, RG2, pathItem, sourceItem, createButton);

		form.redraw();

		addItem(form);
		draw();
	}

}
